package cn.tedu.shoot;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Graphics;

public abstract class FlyingObject {
	public static final int LIFE = 0;
	public static final int DEAD = 1;
	public static final int REMOVE = 2;
	protected int state = LIFE;
	
	protected int width;
	protected int height;
	protected int x;
	protected int y;
	protected int life;
	public FlyingObject(int width,int height){
		this.width = width;
		this.height = height;
		Random rand = new Random();
		x = rand.nextInt(World.WIDTH-this.width);
		y = -this.height;
	}
	public FlyingObject(int width,int height,int x,int y){
		this.width = width;
		this.height = height;
		this.x = x;
		this.y = y;
	}
	/**飞行物移动*/
	public abstract void step(); 
	/**抽象方法*/
	public abstract BufferedImage getImage();
		
	/**判断状态*/
	public boolean isLife() {
		return state==LIFE;
	}
	public boolean isDead() {
		return state==DEAD;
	}
	public boolean isRemove() {
		return state==REMOVE;
	}
	/**画对象g:画笔*/
	public void paintObject(Graphics g) {
		g.drawImage(getImage(),x,y,null);
	}
	/**判断是否越界*/
	public boolean outOfBounds() {
		return this.y>=World.HEIGHT;
	}
	/**判断是否碰撞*/
	public boolean hit(FlyingObject other) {
		int x1 = this.x-other.width;
		int x2 = this.x+this.width;
		int y1 = this.y-other.height;
		int y2 = this.y+this.height;
		int x = other.x;
		int y = other.y;
		return x>=x1 && x<=x2 
				 &&
				 y>=y1 && y<=y2;
	}
	public void goDead() {
		state = DEAD;
	}
	 public static void cutlife(FlyingObject the) {
			the.life--;
		}
	   public static int getLife(FlyingObject the) {
			return the.life;
		}
	   /**返回血条的百分比*/
	   public int downLife(){
		   if(life>5&&life<20){
			   return 4;
		   }else if(life>=20&&life<40){
			   return 3;
		   }else if(life>=40&&life<60){
			   return 2;
		   }else if(life>=60&&life<80){
			   return 1;
		   }else if(this.life>=80){
			   return 0;
		   }
		   return 5;
	   }
	   /**敌人发射子弹*/
	   public ArrayList<FlyingObject> shootingShots(){
		   int sum = 1; 
			int xStep = this.width/(sum+1);
			int yStep = 20;
				BossBullet[] bs = new BossBullet[sum];
				BossBullet b = new BossBullet(10,20);
				int m = b.width/2;
				ArrayList<FlyingObject> c = new ArrayList<>();
				for(int i=0;i<bs.length;i++){
					bs[i] = new BossBullet(this.x+(i+1)*xStep-m,this.y+this.height+yStep);
					c.add(bs[i]);
				}
				return c;
	   }
}
